//
//  CreateJobSpecialAddressView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/3/14.
//

import UIKit
import SnapKit
import RxSwift
import RxCocoa
class CreateJobSpecialAddressView: View {
    lazy var disposeBag = DisposeBag()
    var viewModel = CreateJobInstallAddressViewModel()
    var operBtn: LayoutButton = {
        let view = LayoutButton(.left)
        view.setBtnInfo(R.string.localizable.addSpecialAddress(),hexStr: Constant.ThemeColorHexStr, textFont: R.font.interMedium(size: 14),imageName: "createjob_install_address_special_address_add")
        view.tag = 0
        return view
    }()
    lazy var specialAddressTextView: TextViewWithRequiredView = {
        let view = TextViewWithRequiredView(needCheckFlag: false)
        view.textView.placeholder = "Special address will be shown exactly on STC form. Fill in the actual installation address if you can't select the correct address under the select box."
        view.textView.limitLength = 250
        view.textView.limitLabelFont = R.font.interRegular(size: 14)
        view.textView.limitLabelColor = UIColor(hexStr: "#292D3299")
        return view
    }()
    override func setUpView() {
        self.addSubview(operBtn)
        operBtn.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
            make.height.equalTo(24)
            make.width.greaterThanOrEqualTo(0)
        }
        //
        specialAddressTextView.isHidden = true
        self.addSubview(specialAddressTextView)
        specialAddressTextView.snp.makeConstraints { make in
            make.top.left.right.height.equalTo(operBtn)
            make.bottom.equalToSuperview()
        }
        specialAddressTextView.endEditingClosure = {[weak self] (text) in
            guard let strongSelf = self else { return  }
            strongSelf.viewModel.specialAddress = text
        }
        operBtn.addTarget(self, action: #selector(operateBtnClick(_:)), for: UIControl.Event.touchUpInside)
    }
    @objc func operateBtnClick(_ sender: LayoutButton){
        if sender.tag == 0{//增加special address
            self.addSpecailAddress()
            self.specialAddressTextView.textView.becomeFirstResponder()
        }else{//删除special address
            self.delSpecailAddress()
        }
    }
    func setData(_ model: CreateJobInstallAddressViewModel){
        self.viewModel = model
        if self.viewModel.addSpecialAddressFlag {
            self.addSpecailAddress()
        }else{
            self.delSpecailAddress()
        }
    }
    //MARK: - 增加SpecailAddress
    private func addSpecailAddress(){
        self.viewModel.addSpecialAddressFlag = true
        self.specialAddressTextView.textView.text = self.viewModel.specialAddress
        self.specialAddressTextView.textView.textChange(Notification(name: UITextView.textDidChangeNotification))
        operBtn.setBtnInfo(R.string.localizable.removeSpecialAddress(),hexStr: "#DD890B", textFont: R.font.interMedium(size: 14),imageName: "createjob_install_address_special_address_del")
        operBtn.tag = 1
        specialAddressTextView.isHidden = false
        specialAddressTextView.snp.remakeConstraints { make in
            make.top.equalTo(operBtn.snp.bottom).offset(Constant.TitleTextMargin)
            make.left.right.equalToSuperview()
            make.height.equalTo(96)
            make.bottom.equalToSuperview()
        }
    }
    //MARK: - 删除SpecailAddress
    private func delSpecailAddress(){
        self.viewModel.addSpecialAddressFlag = false
        operBtn.setBtnInfo(R.string.localizable.addSpecialAddress(),hexStr: Constant.ThemeColorHexStr, textFont: R.font.interMedium(size: 14),imageName: "createjob_install_address_special_address_add")
        operBtn.tag = 0
        specialAddressTextView.isHidden = true
        specialAddressTextView.snp.remakeConstraints { make in
            make.top.left.right.height.equalTo(operBtn)
            make.bottom.equalToSuperview()
        }
    }
}
